package cn.ibizlab.odoo.core.odoo_crm.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Icrm_lead;
import cn.ibizlab.odoo.core.odoo_crm.domain.Crm_lead;
import cn.ibizlab.odoo.core.odoo_crm.filter.Crm_leadSearchContext;
import cn.ibizlab.odoo.core.odoo_crm.service.ICrm_leadService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[线索/商机] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Crm_leadServiceImpl implements ICrm_leadService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Icrm_leadClientService crm_leadClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Icrm_lead clientModel = crm_leadClientService.createModel();
        clientModel.setId(id);
        crm_leadClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Crm_lead> crm_leads, int batchSize) {

        //crm_leadClientService.updateBatch(crm_leads,batchSize) ;

    }

    @Override
    public Crm_lead get(Integer id) {
        Icrm_lead clientModel = crm_leadClientService.createModel();
        clientModel.setId(id);
        crm_leadClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Crm_lead get(Crm_lead crm_lead) {
        Crm_lead retcrm_lead = this.get(crm_lead.getId()) ;
    	BeanCopier copier=BeanCopier.create(retcrm_lead.getClass(), crm_lead.getClass(), false);
        copier.copy(retcrm_lead, crm_lead, null);
        return crm_lead ;
    }

    @Override
    public void createBatch(List<Crm_lead> crm_leads, int batchSize) {

                        //crm_leadClientService.createBatch(crm_leads,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //crm_leadClientService.removeBatch(crm_leads) ;

    }

    @Override
    public boolean create(Crm_lead crm_lead) {
        boolean bOk = false ;
        Icrm_lead clientModel = this.convert2Model(crm_lead,null);
        crm_leadClientService.create(clientModel) ;
        this.convert2Domain(clientModel,crm_lead);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Crm_lead crm_lead) {
        boolean bOk = false ;
        Icrm_lead clientModel = this.convert2Model(crm_lead,null);
        crm_leadClientService.update(clientModel) ;
        this.convert2Domain(clientModel,crm_lead) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Crm_lead> searchDefault(Crm_leadSearchContext context) {
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据丢单原因查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_lost(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据CRM 阶段查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_stage(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据销售渠道查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_team(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_company(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据国家/地区州/省查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_state(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据国家/地区查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_country(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据业务伙伴称谓查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_title(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_partner(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_create(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_user(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_write(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM 营销活动查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_campaign(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM媒体查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_medium(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM来源查询
     */
    @Override
    public Page<Crm_lead> selectByOdoo_source(Integer id){
        Crm_leadSearchContext context = new Crm_leadSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Crm_lead> list = new ArrayList<Crm_lead>();
        Page<cn.ibizlab.odoo.core.client.model.Icrm_lead> clientModelList = crm_leadClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icrm_lead clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Crm_lead>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Icrm_lead convert2Model(Crm_lead domain , Icrm_lead model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = crm_leadClientService.createModel() ;
        }

         if(domain.getActivity_user_idDirtyFlag())
            model.setActivity_user_id(domain.getActivity_user_id());
         if(domain.getMeeting_countDirtyFlag())
            model.setMeeting_count(domain.getMeeting_count());
         if(domain.getIbizfunctionDirtyFlag())
            model.setIbizfunction(domain.getIbizfunction());
         if(domain.getMessage_bounceDirtyFlag())
            model.setMessage_bounce(domain.getMessage_bounce());
         if(domain.getCityDirtyFlag())
            model.setCity(domain.getCity());
         if(domain.getDay_closeDirtyFlag())
            model.setDay_close(domain.getDay_close());
         if(domain.getActivity_idsDirtyFlag())
            model.setActivity_ids(domain.getActivity_ids());
         if(domain.getMessage_has_errorDirtyFlag())
            model.setMessage_has_error(domain.getMessage_has_error());
         if(domain.getMessage_unread_counterDirtyFlag())
            model.setMessage_unread_counter(domain.getMessage_unread_counter());
         if(domain.getExpected_revenueDirtyFlag())
            model.setExpected_revenue(domain.getExpected_revenue());
         if(domain.getDate_closedDirtyFlag())
            model.setDate_closed(domain.getDate_closed());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getEmail_ccDirtyFlag())
            model.setEmail_cc(domain.getEmail_cc());
         if(domain.getContact_nameDirtyFlag())
            model.setContact_name(domain.getContact_name());
         if(domain.getDate_last_stage_updateDirtyFlag())
            model.setDate_last_stage_update(domain.getDate_last_stage_update());
         if(domain.getPlanned_revenueDirtyFlag())
            model.setPlanned_revenue(domain.getPlanned_revenue());
         if(domain.getWebsite_message_idsDirtyFlag())
            model.setWebsite_message_ids(domain.getWebsite_message_ids());
         if(domain.getActivity_type_idDirtyFlag())
            model.setActivity_type_id(domain.getActivity_type_id());
         if(domain.getDate_deadlineDirtyFlag())
            model.setDate_deadline(domain.getDate_deadline());
         if(domain.getZipDirtyFlag())
            model.setZip(domain.getZip());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getMobileDirtyFlag())
            model.setMobile(domain.getMobile());
         if(domain.getMessage_idsDirtyFlag())
            model.setMessage_ids(domain.getMessage_ids());
         if(domain.getMessage_partner_idsDirtyFlag())
            model.setMessage_partner_ids(domain.getMessage_partner_ids());
         if(domain.getActivity_stateDirtyFlag())
            model.setActivity_state(domain.getActivity_state());
         if(domain.getActivity_date_deadlineDirtyFlag())
            model.setActivity_date_deadline(domain.getActivity_date_deadline());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getWebsiteDirtyFlag())
            model.setWebsite(domain.getWebsite());
         if(domain.getMessage_attachment_countDirtyFlag())
            model.setMessage_attachment_count(domain.getMessage_attachment_count());
         if(domain.getEmail_fromDirtyFlag())
            model.setEmail_from(domain.getEmail_from());
         if(domain.getDate_conversionDirtyFlag())
            model.setDate_conversion(domain.getDate_conversion());
         if(domain.getPartner_nameDirtyFlag())
            model.setPartner_name(domain.getPartner_name());
         if(domain.getSale_amount_totalDirtyFlag())
            model.setSale_amount_total(domain.getSale_amount_total());
         if(domain.getMessage_unreadDirtyFlag())
            model.setMessage_unread(domain.getMessage_unread());
         if(domain.getMessage_needactionDirtyFlag())
            model.setMessage_needaction(domain.getMessage_needaction());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getKanban_stateDirtyFlag())
            model.setKanban_state(domain.getKanban_state());
         if(domain.getMessage_main_attachment_idDirtyFlag())
            model.setMessage_main_attachment_id(domain.getMessage_main_attachment_id());
         if(domain.getMessage_channel_idsDirtyFlag())
            model.setMessage_channel_ids(domain.getMessage_channel_ids());
         if(domain.getReferredDirtyFlag())
            model.setReferred(domain.getReferred());
         if(domain.getProbabilityDirtyFlag())
            model.setProbability(domain.getProbability());
         if(domain.getDate_action_lastDirtyFlag())
            model.setDate_action_last(domain.getDate_action_last());
         if(domain.getMessage_has_error_counterDirtyFlag())
            model.setMessage_has_error_counter(domain.getMessage_has_error_counter());
         if(domain.getDate_openDirtyFlag())
            model.setDate_open(domain.getDate_open());
         if(domain.getPhoneDirtyFlag())
            model.setPhone(domain.getPhone());
         if(domain.getMessage_is_followerDirtyFlag())
            model.setMessage_is_follower(domain.getMessage_is_follower());
         if(domain.getDay_openDirtyFlag())
            model.setDay_open(domain.getDay_open());
         if(domain.getActivity_summaryDirtyFlag())
            model.setActivity_summary(domain.getActivity_summary());
         if(domain.getSale_numberDirtyFlag())
            model.setSale_number(domain.getSale_number());
         if(domain.getStreet2DirtyFlag())
            model.setStreet2(domain.getStreet2());
         if(domain.getOrder_idsDirtyFlag())
            model.setOrder_ids(domain.getOrder_ids());
         if(domain.getMessage_follower_idsDirtyFlag())
            model.setMessage_follower_ids(domain.getMessage_follower_ids());
         if(domain.getStreetDirtyFlag())
            model.setStreet(domain.getStreet());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getTag_idsDirtyFlag())
            model.setTag_ids(domain.getTag_ids());
         if(domain.getIs_blacklistedDirtyFlag())
            model.setIs_blacklisted(domain.getIs_blacklisted());
         if(domain.getMessage_needaction_counterDirtyFlag())
            model.setMessage_needaction_counter(domain.getMessage_needaction_counter());
         if(domain.getPriorityDirtyFlag())
            model.setPriority(domain.getPriority());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getSource_id_textDirtyFlag())
            model.setSource_id_text(domain.getSource_id_text());
         if(domain.getPartner_address_nameDirtyFlag())
            model.setPartner_address_name(domain.getPartner_address_name());
         if(domain.getMedium_id_textDirtyFlag())
            model.setMedium_id_text(domain.getMedium_id_text());
         if(domain.getCompany_currencyDirtyFlag())
            model.setCompany_currency(domain.getCompany_currency());
         if(domain.getTeam_id_textDirtyFlag())
            model.setTeam_id_text(domain.getTeam_id_text());
         if(domain.getPartner_address_mobileDirtyFlag())
            model.setPartner_address_mobile(domain.getPartner_address_mobile());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getUser_emailDirtyFlag())
            model.setUser_email(domain.getUser_email());
         if(domain.getUser_loginDirtyFlag())
            model.setUser_login(domain.getUser_login());
         if(domain.getPartner_address_phoneDirtyFlag())
            model.setPartner_address_phone(domain.getPartner_address_phone());
         if(domain.getState_id_textDirtyFlag())
            model.setState_id_text(domain.getState_id_text());
         if(domain.getCampaign_id_textDirtyFlag())
            model.setCampaign_id_text(domain.getCampaign_id_text());
         if(domain.getPartner_is_blacklistedDirtyFlag())
            model.setPartner_is_blacklisted(domain.getPartner_is_blacklisted());
         if(domain.getPartner_address_emailDirtyFlag())
            model.setPartner_address_email(domain.getPartner_address_email());
         if(domain.getStage_id_textDirtyFlag())
            model.setStage_id_text(domain.getStage_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getCountry_id_textDirtyFlag())
            model.setCountry_id_text(domain.getCountry_id_text());
         if(domain.getTitle_textDirtyFlag())
            model.setTitle_text(domain.getTitle_text());
         if(domain.getLost_reason_textDirtyFlag())
            model.setLost_reason_text(domain.getLost_reason_text());
         if(domain.getLost_reasonDirtyFlag())
            model.setLost_reason(domain.getLost_reason());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getState_idDirtyFlag())
            model.setState_id(domain.getState_id());
         if(domain.getMedium_idDirtyFlag())
            model.setMedium_id(domain.getMedium_id());
         if(domain.getStage_idDirtyFlag())
            model.setStage_id(domain.getStage_id());
         if(domain.getSource_idDirtyFlag())
            model.setSource_id(domain.getSource_id());
         if(domain.getCountry_idDirtyFlag())
            model.setCountry_id(domain.getCountry_id());
         if(domain.getCampaign_idDirtyFlag())
            model.setCampaign_id(domain.getCampaign_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getTeam_idDirtyFlag())
            model.setTeam_id(domain.getTeam_id());
         if(domain.getTitleDirtyFlag())
            model.setTitle(domain.getTitle());
        return model ;
    }

    public Crm_lead convert2Domain( Icrm_lead model ,Crm_lead domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Crm_lead();
        }

        if(model.getActivity_user_idDirtyFlag())
            domain.setActivity_user_id(model.getActivity_user_id());
        if(model.getMeeting_countDirtyFlag())
            domain.setMeeting_count(model.getMeeting_count());
        if(model.getIbizfunctionDirtyFlag())
            domain.setIbizfunction(model.getIbizfunction());
        if(model.getMessage_bounceDirtyFlag())
            domain.setMessage_bounce(model.getMessage_bounce());
        if(model.getCityDirtyFlag())
            domain.setCity(model.getCity());
        if(model.getDay_closeDirtyFlag())
            domain.setDay_close(model.getDay_close());
        if(model.getActivity_idsDirtyFlag())
            domain.setActivity_ids(model.getActivity_ids());
        if(model.getMessage_has_errorDirtyFlag())
            domain.setMessage_has_error(model.getMessage_has_error());
        if(model.getMessage_unread_counterDirtyFlag())
            domain.setMessage_unread_counter(model.getMessage_unread_counter());
        if(model.getExpected_revenueDirtyFlag())
            domain.setExpected_revenue(model.getExpected_revenue());
        if(model.getDate_closedDirtyFlag())
            domain.setDate_closed(model.getDate_closed());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getEmail_ccDirtyFlag())
            domain.setEmail_cc(model.getEmail_cc());
        if(model.getContact_nameDirtyFlag())
            domain.setContact_name(model.getContact_name());
        if(model.getDate_last_stage_updateDirtyFlag())
            domain.setDate_last_stage_update(model.getDate_last_stage_update());
        if(model.getPlanned_revenueDirtyFlag())
            domain.setPlanned_revenue(model.getPlanned_revenue());
        if(model.getWebsite_message_idsDirtyFlag())
            domain.setWebsite_message_ids(model.getWebsite_message_ids());
        if(model.getActivity_type_idDirtyFlag())
            domain.setActivity_type_id(model.getActivity_type_id());
        if(model.getDate_deadlineDirtyFlag())
            domain.setDate_deadline(model.getDate_deadline());
        if(model.getZipDirtyFlag())
            domain.setZip(model.getZip());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getMobileDirtyFlag())
            domain.setMobile(model.getMobile());
        if(model.getMessage_idsDirtyFlag())
            domain.setMessage_ids(model.getMessage_ids());
        if(model.getMessage_partner_idsDirtyFlag())
            domain.setMessage_partner_ids(model.getMessage_partner_ids());
        if(model.getActivity_stateDirtyFlag())
            domain.setActivity_state(model.getActivity_state());
        if(model.getActivity_date_deadlineDirtyFlag())
            domain.setActivity_date_deadline(model.getActivity_date_deadline());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getWebsiteDirtyFlag())
            domain.setWebsite(model.getWebsite());
        if(model.getMessage_attachment_countDirtyFlag())
            domain.setMessage_attachment_count(model.getMessage_attachment_count());
        if(model.getEmail_fromDirtyFlag())
            domain.setEmail_from(model.getEmail_from());
        if(model.getDate_conversionDirtyFlag())
            domain.setDate_conversion(model.getDate_conversion());
        if(model.getPartner_nameDirtyFlag())
            domain.setPartner_name(model.getPartner_name());
        if(model.getSale_amount_totalDirtyFlag())
            domain.setSale_amount_total(model.getSale_amount_total());
        if(model.getMessage_unreadDirtyFlag())
            domain.setMessage_unread(model.getMessage_unread());
        if(model.getMessage_needactionDirtyFlag())
            domain.setMessage_needaction(model.getMessage_needaction());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getKanban_stateDirtyFlag())
            domain.setKanban_state(model.getKanban_state());
        if(model.getMessage_main_attachment_idDirtyFlag())
            domain.setMessage_main_attachment_id(model.getMessage_main_attachment_id());
        if(model.getMessage_channel_idsDirtyFlag())
            domain.setMessage_channel_ids(model.getMessage_channel_ids());
        if(model.getReferredDirtyFlag())
            domain.setReferred(model.getReferred());
        if(model.getProbabilityDirtyFlag())
            domain.setProbability(model.getProbability());
        if(model.getDate_action_lastDirtyFlag())
            domain.setDate_action_last(model.getDate_action_last());
        if(model.getMessage_has_error_counterDirtyFlag())
            domain.setMessage_has_error_counter(model.getMessage_has_error_counter());
        if(model.getDate_openDirtyFlag())
            domain.setDate_open(model.getDate_open());
        if(model.getPhoneDirtyFlag())
            domain.setPhone(model.getPhone());
        if(model.getMessage_is_followerDirtyFlag())
            domain.setMessage_is_follower(model.getMessage_is_follower());
        if(model.getDay_openDirtyFlag())
            domain.setDay_open(model.getDay_open());
        if(model.getActivity_summaryDirtyFlag())
            domain.setActivity_summary(model.getActivity_summary());
        if(model.getSale_numberDirtyFlag())
            domain.setSale_number(model.getSale_number());
        if(model.getStreet2DirtyFlag())
            domain.setStreet2(model.getStreet2());
        if(model.getOrder_idsDirtyFlag())
            domain.setOrder_ids(model.getOrder_ids());
        if(model.getMessage_follower_idsDirtyFlag())
            domain.setMessage_follower_ids(model.getMessage_follower_ids());
        if(model.getStreetDirtyFlag())
            domain.setStreet(model.getStreet());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getTag_idsDirtyFlag())
            domain.setTag_ids(model.getTag_ids());
        if(model.getIs_blacklistedDirtyFlag())
            domain.setIs_blacklisted(model.getIs_blacklisted());
        if(model.getMessage_needaction_counterDirtyFlag())
            domain.setMessage_needaction_counter(model.getMessage_needaction_counter());
        if(model.getPriorityDirtyFlag())
            domain.setPriority(model.getPriority());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getSource_id_textDirtyFlag())
            domain.setSource_id_text(model.getSource_id_text());
        if(model.getPartner_address_nameDirtyFlag())
            domain.setPartner_address_name(model.getPartner_address_name());
        if(model.getMedium_id_textDirtyFlag())
            domain.setMedium_id_text(model.getMedium_id_text());
        if(model.getCompany_currencyDirtyFlag())
            domain.setCompany_currency(model.getCompany_currency());
        if(model.getTeam_id_textDirtyFlag())
            domain.setTeam_id_text(model.getTeam_id_text());
        if(model.getPartner_address_mobileDirtyFlag())
            domain.setPartner_address_mobile(model.getPartner_address_mobile());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getUser_emailDirtyFlag())
            domain.setUser_email(model.getUser_email());
        if(model.getUser_loginDirtyFlag())
            domain.setUser_login(model.getUser_login());
        if(model.getPartner_address_phoneDirtyFlag())
            domain.setPartner_address_phone(model.getPartner_address_phone());
        if(model.getState_id_textDirtyFlag())
            domain.setState_id_text(model.getState_id_text());
        if(model.getCampaign_id_textDirtyFlag())
            domain.setCampaign_id_text(model.getCampaign_id_text());
        if(model.getPartner_is_blacklistedDirtyFlag())
            domain.setPartner_is_blacklisted(model.getPartner_is_blacklisted());
        if(model.getPartner_address_emailDirtyFlag())
            domain.setPartner_address_email(model.getPartner_address_email());
        if(model.getStage_id_textDirtyFlag())
            domain.setStage_id_text(model.getStage_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getCountry_id_textDirtyFlag())
            domain.setCountry_id_text(model.getCountry_id_text());
        if(model.getTitle_textDirtyFlag())
            domain.setTitle_text(model.getTitle_text());
        if(model.getLost_reason_textDirtyFlag())
            domain.setLost_reason_text(model.getLost_reason_text());
        if(model.getLost_reasonDirtyFlag())
            domain.setLost_reason(model.getLost_reason());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getState_idDirtyFlag())
            domain.setState_id(model.getState_id());
        if(model.getMedium_idDirtyFlag())
            domain.setMedium_id(model.getMedium_id());
        if(model.getStage_idDirtyFlag())
            domain.setStage_id(model.getStage_id());
        if(model.getSource_idDirtyFlag())
            domain.setSource_id(model.getSource_id());
        if(model.getCountry_idDirtyFlag())
            domain.setCountry_id(model.getCountry_id());
        if(model.getCampaign_idDirtyFlag())
            domain.setCampaign_id(model.getCampaign_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getTeam_idDirtyFlag())
            domain.setTeam_id(model.getTeam_id());
        if(model.getTitleDirtyFlag())
            domain.setTitle(model.getTitle());
        return domain ;
    }


}
